home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / WINSRC20.ZIP / ENCODER.C < prev    next >
Text File  |  1990-10-02  |  23KB  |  705 lines

  1. /*
  2.     encoder.c - GIF Encoder and associated routines
  3.     This module is linked as an overlay, use ENTER_OVLY and EXIT_OVLY.
  4. */
  5.  
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "fractint.h"
  10. #include "fractype.h"
  11.  
  12. /* routines in this module    */
  13.  
  14. void encoder_overlay(void);
  15. int  savetodisk(char *);
  16. int  encoder();
  17.  
  18. static void setup_save_info(struct fractal_info *);
  19. static void inittable(void);
  20. static void shftwrite();
  21. static void raster(unsigned int);
  22. static int  extend_blk_len(int datalen);
  23. static void put_extend_blk(int block_id,int block_len,char far *block_data);
  24.  
  25. extern int initbatch;
  26. extern char far *resume_info;        /* pointer to resume info if allocated */
  27. extern int  resume_len;         /* length of resume info */
  28. extern char FormName[40];        /* formula name */
  29. extern int  active_system;        /* 0=dos, 1=windows */
  30.  
  31. extern    int    sxdots,sydots;        /* # of dots on the physical screen    */
  32. extern    int    sxoffs,syoffs;        /* physical top left of logical screen */
  33. extern    int    xdots, ydots;        /* # of dots on the logical screen     */
  34. extern    int    viewwindow;        /* 0 for full screen, 1 for window */
  35. extern    float    finalaspectratio;    /* for view shape and rotation */
  36. extern    int    viewxdots,viewydots;    /* explicit view sizing */
  37. extern    int    colors;         /* maximum colors available */
  38. extern    int    dotmode;        /* so we can detect disk-video */
  39. extern    int    warn;            /* warnings on/off */
  40. extern    int    resave_flag;        /* resaving after a timed save */
  41. extern    int    timedsave;        /* if doing an auto save */
  42. extern    int    disk16bit;        /* 16 bit continuous potential */
  43.  
  44. extern unsigned char dacbox[256][3];    /* Video-DAC (filled in by SETVIDEO) */
  45. extern int    daclearn, daccount;    /* used by the color-cyclers */
  46. extern int    reallyega;        /* "reall-an-EGA" flag */
  47. extern int    extraseg;        /* used by Save-to-GIF routines */
  48. extern int    debugflag;
  49.  
  50. extern int    gif87a_flag;        /* if 1, supress GIF extension blocks */
  51.  
  52. extern int    calc_status;
  53. extern long   calctime;
  54. extern char   stdcalcmode;
  55. extern int    fractype;
  56. extern double xxmin,xxmax;
  57. extern double yymin,yymax;
  58. extern double xx3rd,yy3rd;
  59. extern double param[4];
  60. extern int    maxit;            /* try this many iterations */
  61. extern int    inside;            /* inside color: 1=blue     */
  62. extern int    outside;            /* outside color, if set    */
  63. extern int    finattract;        /* finite attractor option  */
  64. extern int    forcesymmetry;
  65. extern int    LogFlag;            /* non-zero if logarithmic palettes */
  66. extern int    rflag, rseed;
  67. extern int    periodicitycheck;
  68. extern char   useinitorbit;
  69. extern struct complex initorbit;
  70. extern int    pot16bit;
  71. extern float  finalaspectratio;
  72. extern double potparam[3];        /* three potential parameters*/
  73. extern double inversion[];
  74. extern int    decomp[];
  75. extern int    distest;            /* non-zero if distance estimator   */
  76. extern int    init3d[20];        /* '3d=nn/nn/nn/...' values */
  77. extern char   floatflag;        /* floating-point fractals? */
  78. extern int    biomorph;
  79. extern int    bailout;            /* user input bailout value */
  80. extern int    previewfactor;
  81. extern int    xtrans;
  82. extern int    ytrans;
  83. extern int    red_crop_left;
  84. extern int    red_crop_right;
  85. extern int    blue_crop_left;
  86. extern int    blue_crop_right;
  87. extern int    red_bright;
  88. extern int    blue_bright;
  89. extern int    xadjust;
  90. extern int    eyeseparation;
  91. extern int    glassestype;
  92. extern int    save_system;
  93. extern int    save_release;
  94.  
  95. extern int    timer(int timertype,int(*subrtn)(),...);
  96.  
  97. /*
  98.             Save-To-Disk Routines (GIF)
  99.  
  100. GIF and 'Graphics Interchange Format' are trademarks (tm) of Compuserve
  101. Incorporated, an H&R Block Company.
  102.  
  103.  
  104. The following routines perform the GIF encoding when the 's' key is pressed.
  105. The routines refer to several variables that are declared elsewhere
  106. [colors, xdots, ydots, and 'dacbox'], and rely on external routines to
  107. actually read and write screen pixels [getcolor(x,y) and putcolor(x,y,color)].
  108. (Writing pixels is just stuffed in here as a sort of visual status report,
  109. and has nothing to do with any GIF function.)    They also rely on the
  110. existence of an externally-defined 64K dataspace and they use the routines
  111. 'toextra()' and 'cmpextra()' to deal with that dataspace (in the same manner
  112. as 'memcpy()' and 'memcmp()' would).   Otherwise, they perform a generic
  113. GIF-encoder function.
  114.  
  115. Note that these routines use small string- and hash-tables, and "flush"
  116. the GIF entries whenever the hash-table gets two-thirds full or the string
  117. table gets full.   They also use the GIF encoding technique of limiting the
  118. encoded string length to a specific size, "adding" a string to the hash table
  119. at that point even if a matching string exists ("adding" is in quotes, because
  120. if a matching string exists we can increment the code counter but safely throw
  121. the duplicate string away, saving both string space and a hash table entry).
  122.  
  123.    This results in relatively good speed and small data space, but at the
  124. expense of compression efficiency (filesize).    These trade-offs could be
  125. adjusted by modifying the #DEFINEd variables below.
  126.  
  127. Note that the 'strlocn' and 'teststring' routines are declared
  128. to be external just so that they can be defined (and the space re-used)
  129. elsewhere.  The actual declarations are in the assembler code.
  130.  
  131. */
  132.  
  133. #define MAXTEST   100        /* maximum single string length */
  134. #define MAXSTRING 64000     /* total space reserved for strings */
  135.                 /* maximum number of strings available */
  136. #define MAXENTRY  5003        /* (a prime number is best for hashing) */
  137.  
  138. extern unsigned int strlocn[MAXENTRY];
  139. extern unsigned char teststring[MAXTEST];
  140. extern unsigned char block[266];   /* GIF-encoded blocks go here */
  141.  
  142. static int numsaves = 0;    /* For adjusting 'save-to-disk' filenames */
  143.  
  144. static FILE *out;
  145. static int last_colorbar;
  146. static int save16bit;
  147. static int outcolor1s, outcolor2s;
  148.  
  149. static int lentest, lastentry, numentries, numrealentries;
  150. static unsigned int nextentry;
  151. static int clearcode, endcode;
  152. static unsigned int hashcode;
  153.  
  154. static unsigned char blockcount;
  155. static int startbits, codebits, bytecount, bitcount;
  156.  
  157. static char paletteBW[] = {            /* B&W palette */
  158.       0,  0,  0, 63, 63, 63,
  159.     };
  160. static char paletteCGA[] = {            /* 4-color (CGA) palette  */
  161.       0,  0,  0, 21, 63, 63, 63, 21, 63, 63, 63, 63,
  162.     };
  163. static char paletteEGA[] = {            /* 16-color (EGA/CGA) pal */
  164.       0,  0,  0,  0,  0, 42,  0, 42,  0,  0, 42, 42,
  165.      42,  0,  0, 42,  0, 42, 42, 21,  0, 42, 42, 42,
  166.      21, 21, 21, 21, 21, 63, 21, 63, 21, 21, 63, 63,
  167.      63, 21, 21, 63, 21, 63, 63, 63, 21, 63, 63, 63,
  168.     };
  169. int paletteVGA[] = {            /* VGA palette - to DAC registers */
  170.     0, 1, 2, 3, 4, 5,20, 7,56,57,58,59,60,61,62,63,
  171.     };
  172.  
  173. void encoder_overlay() { }    /* for restore_active_ovly */
  174.  
  175. int savetodisk(filename)    /* save-to-disk routine */
  176. char *filename;
  177. {
  178. char tmpmsg[41]; /* before openfile in case of overrun */
  179. char openfile[80], openfiletype[10];
  180. char tmpfile[80];
  181. int newfile;
  182. int i, j, outcolor1, outcolor2, interrupted;
  183.  
  184. ENTER_OVLY(OVLY_ENCODER);
  185.  
  186. if (extraseg == 0) {            /* not enough memory for this */
  187.     buzzer(2);
  188.     EXIT_OVLY;
  189.     return -1;
  190.     }
  191. restart:
  192.  
  193. save16bit = disk16bit;
  194. if (gif87a_flag) /* not storing non-standard fractal info */
  195.     save16bit = 0;
  196.  
  197. strcpy(openfile,filename);        /* decode and open the filename */
  198. strcpy(openfiletype,DEFAULTFRACTALTYPE);/* determine the file extension */
  199. if (save16bit)
  200.     strcpy(openfiletype,".pot");
  201. for (i = 0; i < strlen(openfile); i++)
  202.     if (openfile[i] == '.') {
  203.         strcpy(openfiletype,&openfile[i]);
  204.         openfile[i] = 0;
  205.         }
  206. if (resave_flag == 0)
  207.     updatesavename(filename); /* for next time */
  208.  
  209. strcat(openfile,openfiletype);
  210.  
  211. strcpy(tmpfile,openfile);
  212. if (access(openfile,0) != 0) /* file doesn't exist */
  213.     newfile = 1;
  214. else { /* file already exists */
  215.     if (warn && resave_flag == 0)
  216.         goto restart;
  217.     if (access(openfile,2) != 0) {
  218.         buzzer(2);
  219.         sprintf(tmpmsg," ?? Can't write %s ",openfile);
  220.         texttempmsg(tmpmsg);
  221.         EXIT_OVLY;
  222.         return -1;
  223.         }
  224.     newfile = 0;
  225.     i = strlen(tmpfile);
  226.     while (--i >= 0 && tmpfile[i] != '\\')
  227.         tmpfile[i] = 0;
  228.     strcat(tmpfile,"fractint.tmp");
  229.     }
  230. if ((out=fopen(tmpfile,"wb")) == NULL) {
  231.